package cn.com.cfae.iras.doc.analyze.config;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

/**
 * Created by 佟媛媛 on 18/10/23.
 * RocketMQ相关配置
 */

@Configuration
public class MQProducerConfig {

    private static Logger logger = LoggerFactory.getLogger(MQProducerConfig.class);

    @Value("${rocketmq.namesrv.addr}")
    private String namesrvAddr;
    @Value("${rocketmq.producer.sendtimeout}")
    private Integer producerSendMsgTimeout;
    @Value("${rocketmq.producer.retrytimes}")
    private Integer producerRetryTimes;
    @Value("${rocketmq.group.producer}")
    private String groupProducer;
    @Value("${rocketmq.topic.analyze_status}")
    private String topicAnalyzeStatus;
    @Value("${rocketmq.doc_analyze.producer.status}")
    private Boolean docAnalyzeProducerStatus;



    @Bean(name = "analyzeStatusProducer")
    public DefaultMQProducer getRocketMQProducer() {
        DefaultMQProducer producer = new DefaultMQProducer(groupProducer + "_" + topicAnalyzeStatus);
        producer.setNamesrvAddr(namesrvAddr);
        producer.setInstanceName(System.currentTimeMillis() + "");
        producer.setSendMsgTimeout(producerSendMsgTimeout);
        producer.setRetryTimesWhenSendAsyncFailed(producerRetryTimes);
        try {
            if (docAnalyzeProducerStatus) producer.start();
        } catch (MQClientException e) {
            logger.error(e.getMessage(), e);
        }
        return producer;
    }


}
